(function () {
    const fs = require("fs");
    const archiver = require('archiver');
    let appPath = "./dist", zipPath = "./out", appConfigs = {}, data, folderPath,
        folders = fs.readdirSync(appPath), len = folders?.length, count = 0;
    if (!fs.existsSync(zipPath)) {
        fs.mkdirSync(zipPath);
    }
    folders?.forEach(async (folder) => {
        // console.log(file);
        folderPath = `${appPath}/${folder}`;
        if (fs.statSync(folderPath).isDirectory()) {
            data = fs.readFileSync(`${folderPath}/config.json`, 'utf-8');
            if (data) {
                data = JSON.parse(data);
                appConfigs[folder] = data.version || "";
            }
            //压缩
            const output = fs.createWriteStream(`${zipPath}/${folder}.zip`);
            const archive = archiver('zip', { zlib: { level: 9 } });
            archive.pipe(output);
            archive.directory(folderPath, folder);
            archive.finalize();
            count++;
        }
    });
    function checkStatus() {
        if (count == len) {
            console.log(appConfigs);
            fs.writeFileSync(`${zipPath}/config.json`, JSON.stringify(appConfigs));
        } else {
            setTimeout(() => {
                checkStatus();
            }, 500);
        }
    }
    checkStatus();
})();